/**
 * Copyright (C) 2014 Domon.Xie All Rights Reserved.
 *
 * ModelRegistry.java
 * 2014年10月20日 上午1:16:13
 */
package org.domon.mybatis.bind;

import java.util.HashMap;
import java.util.Map;

import org.domon.mybatis.sql.DbNameGenerator;

/**
 * @author Domon.Xie
 * 
 */
public class ModelRegistry {

    private DbNameGenerator dbNameGenerator;

    /**
     * 表名集合
     */
    private final Map<Class<?>, String> tableNameMap;

    /**
     * 表主键集合
     */
    private final Map<Class<?>, String> tableKeyMap;

    /**
     * 表插入sql集合
     */
    private final Map<Class<?>, String> insertSqlMap;

    /**
     * 表删除sql集合
     */
    private final Map<Class<?>, String> deleteSqlMap;

    /**
     * 表等式sql集合
     * eg: USER_NAME=#{userName}
     */
    private final Map<Class<?>, Map<String, String>> equalsSqlMap;

    public ModelRegistry() {
        this.tableNameMap = new HashMap<Class<?>, String>();
        this.tableKeyMap = new HashMap<Class<?>, String>();
        this.insertSqlMap = new HashMap<Class<?>, String>();
        this.deleteSqlMap = new HashMap<Class<?>, String>();
        this.equalsSqlMap = new HashMap<Class<?>, Map<String, String>>();
    }

    public ModelRegistry(ModelRegistry modelRegistry, DbNameGenerator dbNameGenerator) {
        this.tableNameMap = modelRegistry.getTableNameMap();
        this.tableKeyMap = modelRegistry.getTableKeyMap();
        this.insertSqlMap = modelRegistry.getInsertSqlMap();
        this.deleteSqlMap = modelRegistry.getDeleteSqlMap();
        this.equalsSqlMap = modelRegistry.getEqualsSqlMap();
        this.dbNameGenerator = dbNameGenerator;
    }

    public Map<Class<?>, String> getTableNameMap() {
        return tableNameMap;
    }

    public void addTableName(Class<?> beanClass, String tableName) {
        this.tableNameMap.put(beanClass, tableName);
    }

    public Map<Class<?>, String> getTableKeyMap() {
        return tableKeyMap;
    }

    public void addTableKey(Class<?> beanClass, String tableKey) {
        this.tableKeyMap.put(beanClass, tableKey);
    }

    public Map<Class<?>, String> getInsertSqlMap() {
        return insertSqlMap;
    }

    public void addInsertSql(Class<?> beanClass, String insertSql) {
        this.insertSqlMap.put(beanClass, insertSql);
    }

    public Map<Class<?>, String> getDeleteSqlMap() {
        return deleteSqlMap;
    }

    public void addDeleteSql(Class<?> beanClass, String deleteSql) {
        this.deleteSqlMap.put(beanClass, deleteSql);
    }

    public Map<Class<?>, Map<String, String>> getEqualsSqlMap() {
        return equalsSqlMap;
    }

    public void addEqualsSql(Class<?> beanClass, Map<String, String> equalsMap) {
        this.equalsSqlMap.put(beanClass, equalsMap);
    }

    /**
     * 根据beanClass获取表名
     * 
     * @param beanClass
     * @return
     */
    public String getTableName(Class<?> beanClass) {
        return tableNameMap.get(beanClass);
    }

    /**
     * 根据beanClass获取主键
     * 
     * @param beanClass
     * @return
     */
    public String getTableKey(Class<?> beanClass) {
        return tableKeyMap.get(beanClass);
    }

    /**
     * 根据beanClass获取修改sql
     * 
     * @param beanClass
     * @return
     */
    public String getDeleteSqlMap(Class<?> beanClass) {
        return deleteSqlMap.get(beanClass);
    }

    /**
     * 根据beanClass获取插入sql
     * 
     * @param beanClass
     * @return
     */
    public String getInsertSql(Class<?> beanClass) {
        return insertSqlMap.get(beanClass);
    }

    /**
     * 根据beanClass 字段名获取等式sql
     * 
     * @param beanClass
     * @param fieldName
     * @return
     */
    public String getEqualsSql(Class<?> beanClass, String fieldName) {
        return equalsSqlMap.get(beanClass).get(fieldName);
    }

    /**
     * 根据beanClass 修改字段名获取修改sql
     * @param beanClass
     * @param fieldNames
     * @return
     */
    public String getUpdateSql(Class<?> beanClass, String... fieldNames) {
        if (fieldNames == null || fieldNames.length == 0) {
            return null;
        }
        return dbNameGenerator.generateUpdateSql(this, beanClass, fieldNames);
    }
}
